package com.qinhailin.portal.business.service;

import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;
import com.qinhailin.portal.business.model.ICustomerInfo;
import com.qinhailin.common.base.service.BaseService;
import com.qinhailin.common.kit.IdKit;
import com.qinhailin.common.vo.Grid;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.node.ArrayNode;
import org.codehaus.jackson.node.ObjectNode;

/**
 * 
 * @author qinhailin
 * @date 2024-12-12
 */
public class ICustomerInfoService extends BaseService {
	private ICustomerInfo dao = new ICustomerInfo().dao();

    @Override
  	public Model<ICustomerInfo> getDao(){
    	return dao;
   	}


   	public Grid page(int pageNumber, int pageSize,Record record) {
      	Record rd = new Record();
      	rd.set("devideid=", record.getStr("refId"));
      	return queryForList(pageNumber, pageSize,rd);
   	}
   	
   	public boolean saveTableList(JSONArray iCustomerInfoList,String refId){
   		for(int i=0;i<iCustomerInfoList.size();i++){
			Map<String, Object> columns=(JSONObject) iCustomerInfoList.get(i);
			Record record =new Record();
			record.setColumns(columns);
			record.set("devideid", refId);
			record.remove("LAY_TABLE_INDEX","LAY_CHECKED","LAY_INDEX","LAY_NUM");
			if(isExists(getPK(),columns.get(getPK())+"")){
				Db.update(getTable(), record);
			}else{
				record.set(getPK(), IdKit.createUUID());
				Db.save(getTable(), record);				
			}
		}
   		return true;
   	}

   	public ObjectNode queryMsg(){
		Record numberofc = Db.findFirst(Db.getSql("core.numberofc"));
		Record numberofnewc = Db.findFirst(Db.getSql("core.numberofnewc"));
		Record numberofw = Db.findFirst(Db.getSql("core.numberofw"));
		Record numberofneww = Db.findFirst(Db.getSql("core.numberofneww"));
		Record numberofd = Db.findFirst(Db.getSql("core.numberofd"));
		Record numberofnewd = Db.findFirst(Db.getSql("core.numberofnewd"));
		ObjectMapper mapper = new ObjectMapper();
		ObjectNode json = mapper.createObjectNode();
		json.put("numberofc",numberofc.getStr("numberofc"));
		json.put("numberofnewc",numberofnewc.getStr("numberofnewc"));
		json.put("numberofw",numberofw.getStr("numberofw"));
		json.put("numberofneww",numberofneww.getStr("numberofneww"));
		json.put("numberofd",numberofd.getStr("numberofd"));
		json.put("numberofnewd",numberofnewd.getStr("numberofnewd"));
		return json;
	}

	public ObjectNode queryWarnInfo(){
		ObjectMapper mapper = new ObjectMapper();

		Record numberofc = Db.findFirst(Db.getSql("core.numberofc"));
		Record numberofw = Db.findFirst(Db.getSql("core.numberofw"));
		Record numberofd = Db.findFirst(Db.getSql("core.numberofd"));
		List<Record> queryWarnLists = Db.find(Db.getSql("core.queryWarnLists"));
		ArrayNode warnArrayNode = mapper.createArrayNode();
		for (Record record:queryWarnLists) {
			ObjectNode arg = mapper.createObjectNode();
			arg.put("warningtime",record.getStr("warningtime"));
			arg.put("message",record.getStr("message"));
			arg.put("id",record.getStr("id"));
			warnArrayNode.add(arg);
		}

		List<Record> queryCountWarnLists = Db.find(Db.getSql("core.queryCountWarnLists"));
		ArrayNode warnCountArrayNode = mapper.createArrayNode();
		for (Record record:queryCountWarnLists) {
			ObjectNode arg = mapper.createObjectNode();
			arg.put("name",record.getStr("name"));
			arg.put("value",record.getStr("value"));
			warnCountArrayNode.add(arg);
		}

		Record numberofnewwofM = Db.findFirst(Db.getSql("core.numberofnewwofM"));


		Record yearCountOfWarn = Db.findFirst(getWarnSql("1 YEAR"));
		Record threeMonthCountOfWarn = Db.findFirst(getWarnSql("3 MONTH"));
		Record oneMonthCountOfWarn = Db.findFirst(getWarnSql("1 MONTH"));
		Record dayCountOfWarn = Db.findFirst(getWarnSql("1 DAY"));
		Record yearCountOfCust = Db.findFirst(getCustSql("1 YEAR"));
		Record threeMonthCountOfCust = Db.findFirst(getCustSql("3 MONTH"));
		Record oneMonthCountOfCust = Db.findFirst(getCustSql("1 MONTH"));
		Record dayCountOfCust = Db.findFirst(getCustSql("1 DAY"));
		ObjectNode countjson = mapper.createObjectNode();
		ObjectNode arr365 = mapper.createObjectNode();
		arr365.put("orders",yearCountOfWarn.getStr("nums"));
		arr365.put("amount",yearCountOfCust.getStr("nums"));
		ObjectNode arr90 = mapper.createObjectNode();
		arr90.put("orders",threeMonthCountOfWarn.getStr("nums"));
		arr90.put("amount",threeMonthCountOfCust.getStr("nums"));
		ObjectNode arr30 = mapper.createObjectNode();
		arr30.put("orders",oneMonthCountOfWarn.getStr("nums"));
		arr30.put("amount",oneMonthCountOfCust.getStr("nums"));
		ObjectNode arr1 = mapper.createObjectNode();
		arr1.put("orders",dayCountOfWarn.getStr("nums"));
		arr1.put("amount",dayCountOfCust.getStr("nums"));
		countjson.put("day365",arr365);
		countjson.put("day90",arr90);
		countjson.put("day30",arr30);
		countjson.put("day1",arr1);





		ObjectNode json = mapper.createObjectNode();
		json.put("numberofc",numberofc.getStr("numberofc"));
		json.put("numberofw",numberofw.getStr("numberofw"));
		json.put("numberofd",numberofd.getStr("numberofd"));
		json.put("numberofnewwofM",numberofnewwofM.getStr("numberofnewwofM"));
		json.put("warnArrayNode",warnArrayNode);
		json.put("warnCountArrayNode",warnCountArrayNode);
		json.put("countjson",countjson);

		return json;
	}

	private String getWarnSql(String parten){
    	return "SELECT count(1) as nums FROM i_warning_list  WHERE warningtime BETWEEN DATE_SUB(NOW(), INTERVAL "+parten+") AND NOW()";
	}

	private String getCustSql(String parten){
		return "select  count(1) as nums from i_customer_info ici WHERE inputtime BETWEEN DATE_SUB(NOW(), INTERVAL "+parten+") AND NOW()";
	}
}
